﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace RssReader
{
	/// <summary>
	/// Interaction logic for Window2.xaml
	/// </summary>
	public partial class Window2 : Window
	{
		public Window2()
		{
			this.InitializeComponent();

            ViewModel vm = new ViewModel();
      
            vm.Feeds.Add(new Feed("http://blogs.msdn.com/tims/rss.xml"));
            vm.Feeds.Add(new Feed("http://blogs.msdn.com/jaimer/rss.xml"));
            this.DataContext = vm;
           // vm.SelectedFeed = vm.Feeds[0]; 


			// Insert code required on object creation below this point.
		}

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ViewModel vm = this.DataContext as ViewModel;

            Feed f = vm.SelectedFeed;
            if (f != null && f.Posts.Count > 0)
            {
                Post p = f.Posts[0];

                StringBuilder sb = new StringBuilder();
                sb.Append("<HTML><HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></HEAD><BODY>Tes'sdd");
                sb.Append(p.Description);
                sb.Append("</BODY><HTML>"); 

                this.webBrowser.NavigateToString(sb.ToString());


                string XAML = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(p.Description, false);

                MemoryStream memory = new MemoryStream(XAML.Length);
                StreamWriter writer = new StreamWriter(memory);
                writer.Write(XAML);
                writer.Flush();
                memory.Seek(0, SeekOrigin.Begin);


                Section obj = System.Windows.Markup.XamlReader.Load(memory) as Section;
                feeds.Blocks.Clear(); 
                feeds.Blocks.Add(obj);
            } 
        }
	}
}